package zserio.extension.python;

import zserio.extension.common.ExpressionFormatter;
import zserio.extension.common.ExpressionFormattingPolicy;
import zserio.extension.common.PackedTypesCollector;

/**
 * FreeMarker template data context for all emitters.
 *
 * Holds all parameters used by FreeMarker template data.
 */
class TemplateDataContext
{
    public TemplateDataContext(
            PythonExtensionParameters pythonParameters, PackedTypesCollector packedTypesCollector)
    {
        this.packedTypesCollector = packedTypesCollector;
        pythonNativeMapper = new PythonNativeMapper();
        withWriterCode = pythonParameters.getWithWriterCode();
        withRangeCheckCode = pythonParameters.getWithRangeCheckCode();
        withTypeInfoCode = pythonParameters.getWithTypeInfoCode();
        withCodeComments = pythonParameters.getWithCodeComments();
        generatorDescription = "# Automatically generated by Zserio Python generator version " +
                PythonExtensionVersion.PYTHON_EXTENSION_VERSION_STRING + " using Zserio core " +
                pythonParameters.getZserioVersion() + ".\n"
                + "# Generator setup: " + pythonParameters.getParametersDescription();
    }

    public PackedTypesCollector getPackedTypesCollector()
    {
        return packedTypesCollector;
    }

    public PythonNativeMapper getPythonNativeMapper()
    {
        return pythonNativeMapper;
    }

    public ExpressionFormatter getPythonExpressionFormatter(ImportCollector importCollector)
    {
        final ExpressionFormattingPolicy expressionFormattingPolicy =
                new PythonExpressionFormattingPolicy(this, importCollector);

        return new ExpressionFormatter(expressionFormattingPolicy);
    }

    public ExpressionFormatter getPythonOwnerIndirectExpressionFormatter(ImportCollector importCollector)
    {
        final ExpressionFormattingPolicy expressionFormattingPolicy =
                new PythonExpressionFormattingPolicy(this, importCollector, PYTHON_OWNER_PREFIX);

        return new ExpressionFormatter(expressionFormattingPolicy);
    }

    public ExpressionFormatter getPythonSqlIndirectExpressionFormatter(ImportCollector importCollector)
    {
        final ExpressionFormattingPolicy expressionFormattingPolicy =
                new PythonSqlIndirectExpressionFormattingPolicy(this, importCollector);

        return new ExpressionFormatter(expressionFormattingPolicy);
    }

    public boolean getWithWriterCode()
    {
        return withWriterCode;
    }

    public boolean getWithRangeCheckCode()
    {
        return withRangeCheckCode;
    }

    public boolean getWithTypeInfoCode()
    {
        return withTypeInfoCode;
    }

    public boolean getWithCodeComments()
    {
        return withCodeComments;
    }

    public String getGeneratorDescription()
    {
        return generatorDescription;
    }

    protected static final String PYTHON_OWNER_PREFIX = "self._owner.";

    private final PackedTypesCollector packedTypesCollector;

    private final PythonNativeMapper pythonNativeMapper;

    private final boolean withWriterCode;
    private final boolean withRangeCheckCode;
    private final boolean withTypeInfoCode;
    private final boolean withCodeComments;
    private final String generatorDescription;
}
